<?php 

require 'action/functions.php';
require_once 'data/data.php';

$data = @query('select ID,Number,Name,IDcard,(year(now())-year(Birth)) as Age,Gender,Birth,Grade,University,School,Major,GPA,Introduction from students');

if ($_SERVER['REQUEST_METHOD'] === 'POST'){
  $searchkey = isset($_POST['search_button'])?trim(htmlspecialchars($_POST['search_key'])):'';
  $searchData = [];
  foreach ($data as $row){
    if (stripos($row['Name'], $searchkey) !== false) {
      $searchData[] = $row;
    }else if ($searchkey == '') {
      $searchData[] = $row;
    }
  }
  $data = $searchData;
}


if(isset($_POST['sort_button'])){
  switch ($_POST['sort_key']) {
    case 0:
      usort($data,'compareById');
      break;
    case 1:
        usort($data,'compareByName');
        break;
    case 2:
        usort($data,'compareByAge');
        break;    
    default:
      break;
  }
}
?>
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title></title>
  <style type="text/css">
  	body{}
  	#table{
  	  min-width: 800px;
  	  border: 1px solid #999;
  	  margin: 10px auto;
  	  text-align: center;
  	}
    a{
      color: #777;
      text-decoration: none;
    }
    a:hover {
      color: #9ce;
    }
    a.submit{
      display: inline-block;
      height: 45px;
      line-height: 45px;
      text-align: center;
      padding: 0px 18px;
      margin: 10px 0px;
      background: #cecece;
      color: #777;
      text-decoration: none;
      border-radius: 4px;
    }
    a.submit:hover {
      background: #dedede;
      color: #999;
    }
  </style>
</head>
<body>
  <form method="post">
  	<input type="text" name="search_key" placeholder="key word">
  	<input type="submit" name="search_button"><br/>
  	<select name="sort_key">
  		<option value="0">按id</option>
  		<option value="1">按name</option>
  		<option value="2">按age</option>
  	</select>
  	<input type="submit" name="sort_button"><br/>
  </form>
  <table id="table" border="1" cellspacing="0" cellpadding="2">
  	<tr>
  	  <th>ID</th>
  	  <th>Number</th>
  	  <th>Name</th>
  	  <th>IDcard</th>
  	  <th>Age</th>
  	  <th>Gender</th>
      <th>Birth</th>
      <th>Grade</th>
      <th>University</th>
      <th>School</th>
      <th>Major</th>
      <th>GPA</th>
      <th>Introduction</th>
      <th>actions</th>
  	</tr>
    <?php foreach ($data as $row): ?>
    <tr>
      <td><?=$row['ID'] ?></td>
      <td><?=$row['Number'] ?></td>
      <td><?=$row['Name'] ?></td>
      <td><?=$row['IDcard'] ?></td>
      <td><?=$row['Age'] ?></td>
      <td><?=$row['Gender'] ?></td>
      <td><?=$row['Birth'] ?></td>
      <td><?=$row['Grade'] ?></td>
      <td><?=$row['University'] ?></td>
      <td><?=$row['School'] ?></td>
      <td><?=$row['Major'] ?></td>
      <td><?=$row['GPA'] ?></td>
      <td><?=$row['Introduction'] ?></td>
      <td><a href="stu_info.php?op=2&id=<?= $row['ID'] ?>">编辑 </a><a href="action/delete.php?id=<?= $row['ID'] ?>"> 删除</a></td>
    </tr>
    <?php endforeach; ?>
  </table>
  <div>
      <a class="submit" href="stu_info.php?op=1">user add</a>
  </div>
</body>
</html>